<?php
/**
 *
 */
class Context extends DynamicObject
{
	public $application;
	public $service;
	public $params;

	public function __construct( AbstractApplication $application )
	{
		$this->application = $application;
	}

	public function initialize( $request, $service )
	{
		// Define los parámetros predeterminados de respuesta.
		//
		ServiceResponse::setValue("service", $service);
		ServiceResponse::setValue("flags", 0);
		ServiceResponse::setValue("error", 0);
		ServiceResponse::setValue("responseType", $request["responseType"] );

		// Determina el tipo de respuesta.
		//
		if ( ServiceResponse::getValue("responseType") == "object" )
		{
			ServiceResponse::$streamMode = ServiceResponse::OUTPUT_BUFFER;
		}
		else {
			ServiceResponse::$streamMode = ServiceResponse::OUTPUT_SERVICE;
		}

		$config = ContextLoader::getInstance()->configure;

		// Obtiene el objeto Auth para este contexto.
		//
		$clases = $config->application;

		for( $i = 0; $i < count($config->userLibs); $i++)
		{
			$class = $config->userLibs[$i];

			$prop = strtolower($class);

			$this->$prop = ClassLoader::getInstance()->uniqueInstance($class);
		}

		$this->service = $service;

		// Crea las propiedades dinámicas.
		//
		if ( is_array($this->params) )
		{
			foreach(array_keys($this->params) as $key)
			{
				$this->$key = $this->params[$key];
			}
		}

		if ( $request )
		{
			foreach ( array_keys($request) as $key )
			{
				$this->$key = $request[$key];
			}
		}
	}

	public static function merge(Context $source, Context $target, $overwrite = false)
	{
		foreach( $source as $property => $value ) {
			if ( property_exists($target, $property) && !$overwrite )
				continue;

			$target->$property = $value;
		}
	}
}

?>
